<%-- 
    Document   : movie
    Created on : Apr 22, 2013, 2:27:53 PM
    Author     : Lord Makuta
--%>

<%@page import="db.UserDAO"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.FileNotFoundException"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="db.DB"%>
<%@page import="db.MovieObj"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <%
        // Please add some comments here about what's going on in this scriptlet
        int mId=Integer.parseInt(request.getParameter("id"));
        int numVotes=0;
        MovieObj mobj=null;
        String clrClass="";
        
        DB data=new DB();
        Connection conn=null;
        Statement stm=null;
        ResultSet res=null;
        ResultSet actRes=null;
        try
        {
            conn=data.getConnection();
            stm=conn.createStatement();
            res=stm.executeQuery("SELECT Name, Type, Rating, NumCopies FROM Movie WHERE Id="+mId);
            res.next();
            mobj=new MovieObj(mId, res.getString(1), res.getString(2));
            mobj.setRating(res.getDouble(3));
            mobj.setAvailable(res.getInt(4));
            
            if (mobj.getGenre().equals("Comedy"))
            {
                clrClass="comedyClr";
            }
            else if (mobj.getGenre().equals("Drama"))
            {
                clrClass="dramaClr";
            }
            else if (mobj.getGenre().equals("Action"))
            {
                clrClass="actionClr";
            }
            else if (mobj.getGenre().equals("Foreign"))
            {
                clrClass="foreignClr";
            }
            
            pageContext.setAttribute("txtClr", clrClass);
            pageContext.setAttribute("movie", mobj);
            
            //actRes=stm.executeQuery("SELECT a.Name, a.Id FROM Actor a, AppearedIn i WHERE a.Id=i.ActorId AND i.MovieId="+mId);
            
            res=stm.executeQuery("SELECT COUNT(*) FROM MovieRatings WHERE MovieId="+mId);
            res.next();
            numVotes=res.getInt(1);
            
        }
        catch (Exception e)
        {
            
        }
        finally
        {
            conn=null;
        }
    %>
    <head>
        <title>DataFlix - <%=mobj.getTitle()%></title>
	
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<meta http-equiv="content-language" content="en" />
	<meta name="description" content="" />
	<meta name="language" content="en" />
	
	<link rel="shortcut icon" href="http://www.iconarchive.com/download/i59247/franksouza183/fs/Apps-firefox.ico"/>
	
	<link type="text/css" rel="stylesheet" href="./styles/fonts.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/header.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/fancybox.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/images.css"/>

	<link type="text/css" rel="stylesheet" href="./styles/charcoal.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/moviepage.css"/>
	
	<script type="text/javascript">
		function show(divID)
		{
			divID.className='hidden';
			document.getElementById('actHid').className='unhidden';
		}
                
	</script>
    </head>
    <body>
        
        <jsp:include page="./header.jsp" />
        
        <div id="movieDesc">
		
		<a class="fancyBox" <%="id=\"movie"+mobj.getId()+"\""%> <%="style=\"background-image: url(./images/movies/"+mobj.getGenre().toLowerCase()+"/movie"+mobj.getId()+".jpg\""%>></a>
		
		<div id="info">
			<h1><%=mobj.getTitle()%></h1>
			<p id="genreInfo">Genre: <span class="${txtClr}"><a <%="href=\"category.jsp?genre="+mobj.getGenre()+"\""%>><%=mobj.getGenre()%> </a></span></p>
			<p id="rating">User rating: <span class="${txtClr}">
                                <%  for (int v=0; v<mobj.getRating(); v++)
                                    {%> &#9733; <%}%></span></p>
			<p id="rateStat">Out of <%=numVotes%> votes: ${movie.getRating()} stars</p>
		
			<ul id="actLs">
                            <li id="actTl">Actors</li>
                                <%
                                    int a=0;
                                    actRes=stm.executeQuery("SELECT a.Name, a.Id FROM Actor a, AppearedIn i WHERE a.Id=i.ActorId AND i.MovieId="+mId);
                                    while(actRes.next())
                                    {
                                        if (a<4)
                                        {
                                    %>
                                        <li><span class="${txtClr}"><a <%="href=\"actor.jsp?id="+actRes.getInt(2)+"\"" %>><%=actRes.getString(1)%></a></span></li>
                                    <%      
                                        }
                                        else
                                            break;
                                        a++;
                                    }
                                %>
                            <%
                                if (a>=4)
                                {
                            %>
                            <li id="actMore"><span class="${txtClr}"><a href="#" onclick="show(this)">Show more...</a></span></li>
                            <div class="hidden" id="actHid">
                            <%
                                    while (actRes.next())
                                    {
                                %>
                                    <li><a <%="href=\"actor.jsp?id="+actRes.getInt(2)+"\"" %>><%=actRes.getString(1)%></a></li>
                                <%
                                    }
                                    out.write("</div>");
                                }
                            %>
                                
			</ul>
		</div>
		
		<p id="summary"> 
                    <%
                        String s=application.getRealPath("/");
                        File f=new File(s+"images/movies/"+mobj.getGenre().toLowerCase()+"/movie"+mobj.getId()+"Desc.txt");
                        FileInputStream fis=new FileInputStream(f);
                        InputStreamReader inStream = new InputStreamReader(fis);
			BufferedReader buff = new BufferedReader(inStream);
			
                        s=buff.readLine();
                        out.print(s);
                    %>
		</p>
	</div>
	
	<div id="orderSect">
            <h1>Availability: <span>${movie.getAvailable()} Copies Available</span></h1>
            <form method="post" action="http://localhost:8080/dataflix/queue.jsp?id=${movie.getId()}">        <%--${movie.getAvailable()>0 && user!=null?"value=\"Add to Queue\"":"value=\"Please Log In\""}--%>                
                <input ${movie.getAvailable()>0 && user!=null?"":"disabled=disabled"} id="Qbtn" type="submit" value="Add to Queue" class=<%="\""+mobj.getGenre().toLowerCase()+"Btn"+"\""%>/></form>
	</div>
	
        <%
            try
            {
                conn=data.getConnection();
                stm=conn.createStatement();
                String checker="SELECT COUNT(*) FROM MovieRatings WHERE CustomerId="+((UserDAO)session.getAttribute("user")).getId()+" AND MovieId="+mobj.getId();
                res=stm.executeQuery(checker);
                res.next();
                if (res.getInt(1)<1)
                {
                    %>
            <div id="score">
                <form action="rater.jsp">
                    <h1>Give this movie a rating?</h1>
                    <select name="score">
                        <option value="" selected="selected">Your rating</option>
                        <option value="1">&#9733;</option>
                        <option value="2">&#9733;&#9733;</option>
                        <option value="3">&#9733;&#9733;&#9733;</option>
                        <option value="4">&#9733;&#9733;&#9733;&#9733;</option>
                        <option value="5">&#9733;&#9733;&#9733;&#9733;&#9733;</option>
                    </select>
                    <input type="hidden" name="movie" value="${movie.getId()}" />
                    <input id="rateSub" name="rateSub" value="Submit rating" type="submit"/>
                </form>
            </div>
                    <%
                }
            }
            catch (Exception e)
            {
                
            }
        %>
                
                
                
        <%
            MovieObj[] mobjs=new MovieObj[6];
            try
            {
                //conn=data.getConnection();
                stm=conn.createStatement();
                res=stm.executeQuery("SELECT Id, Name, Type FROM Movie WHERE Type='"+mobj.getGenre()+"' AND Id!="+mobj.getId());
                ArrayList<MovieObj> alGen=new ArrayList<MovieObj>();
                
                while (res.next())
                {
                    MovieObj tmp=new MovieObj(res.getInt(1), res.getString(2), res.getString(3));
                    alGen.add(tmp);
                }
                
                for (int i=0; i<6; i++)
                {
                    int index=(int)(Math.random()*alGen.size());
                    mobjs[i]=alGen.get(index);
                    alGen.remove(index);
                }
                
                alGen.clear();
                
            }
            catch (Exception e)
            {

            }
        %>
	<ul class="contentRow" <%="id=\""+mobj.getGenre()+"\""%> >
            <h1>Other <%=mobj.getGenre()%> Films</h1>
                <%
                        for (int j=0; j<6; j++)
                        {   
                        %>                       
                        <li <%if (j-3>=0) out.write("class=\"expLef\"");%> ><a class="fancyBox" <%="href=\"./movie.jsp?id="+mobjs[j].getId()+"\""%> >
				<img <%="id=\"movie"+mobjs[j].getId()+"\""%> <%="style=\"background-image: url(./images/movies/"+mobjs[j].getGenre().toLowerCase()+"/movie"+mobjs[j].getId()+".jpg\""%>/>
				<div class="overlay">
					<h1><%=mobjs[j].getTitle()%></h1>
					<div class="shortDesc">
                                            <%
                                                s=application.getRealPath("/");
                                                f=new File(s+"images/movies/"+mobj.getGenre().toLowerCase()+"/movie"+mobjs[j].getId()+"Desc.txt");
                                                fis=new FileInputStream(f);
                                                inStream = new InputStreamReader(fis);
                                                buff = new BufferedReader(inStream);

                                                s=buff.readLine();
                                                out.print(s.substring(0, 205)+"...");
                                            %>
					</div>
				</div>
			</a></li>
                        <%
                        }
                        conn.close();
                %>
	</ul>
        
    </body>
</html>
